#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/7/13 15:00
# @Author  : sujun
# @File    : parameventparse.py
# @Software: PyCharm

import pdb
import json
import yaml
from conftest import logging
from src.utils.common import load_yaml_config
from src.core.Status.AIBSSessionStatus import AIBSParam


class ParamEventParse:
    def __init__(self, filename) -> None:
        self.events = load_yaml_config(yaml_config=filename)
        self.allParam = None
        self.allInput = None
        self.event_keys = self.__load_keys()

    def __load_keys(self):
        info = dict()
        self.allParam = {**self.events['ParamStringOrInt'], **self.events['ParamFile']}
        self.allInput = {**self.events['InputStringOrInt'], **self.events['InputFile']}
        info['param'] = self.allParam.keys()
        info['input'] = self.allInput.keys()
        return info

    def dealParamEvent(self, session, params):
        for key in params.keys():
            paramValue = params.get(key)
            try:
                if key in self.allParam:
                    if key in self.events['ParamStringOrInt'].keys():
                        keyValue = self.events['ParamStringOrInt'][key]
                        if "%d" in keyValue:
                            paramValue = int(paramValue)
                        data = eval(keyValue%paramValue)
                        logging.info("send param event:"+str(data)+str(type(data)))
                        session.set_event(data)
                    elif key in self.events['ParamFile'].keys():
                        val = self.events['ParamFile'][key].copy()
                        with open(paramValue,'r') as f1:
                            json_info = json.load(f1)
                            val[self.getKey(val,'paramfile')] = json_info
                        logging.info("send param event file:"+str(val)+str(type(val)))
                        session.set_event(val)
                else:
                    continue
            except Exception as e:
                logging.error(f">>>>>ERROR： {key} not in PARAM_EVENT_PATH_CONFIG file. error:{e}\n")
                raise ValueError(f">>>>>ERROR： {key} not in PARAM_EVENT_PATH_CONFIG file. error:{e}\n")

    def getKey(self, val, key):
        for i in val.keys():
            if val[i] == key:
                return i
        return -1

    def dealInputEvent(self, session, input):
        for key in input.keys():
            inputValue = input.get(key)
            try:
                if key in self.allInput:
                    if key in self.events['InputStringOrInt']:
                        keyValue = self.events['InputStringOrInt'][key]
                        if "%d" in keyValue:
                            inputValue = int(inputValue)
                        data = eval(keyValue%inputValue)
                        logging.info("send input event:"+str(data)+str(type(data)))
                        session.set_event(data)
                    elif key in self.events['InputFile']:
                        val = self.events['InputFile'][key].copy()
                        with open(inputValue, 'r') as f1:
                            json_info = json.load(f1)
                            val[self.getKey(val, 'paramfile')] = json_info
                        logging.info("send input event file:"+str(val)+str(type(val)))
                        session.set_event(val)
                else:
                    continue
            except Exception as e:
                logging.error(f">>>>>ERROR： {key} not in PARAM_EVENT_PATH_CONFIG file. error:{e}\n")
                raise ValueError(f">>>>>ERROR： {key} not in PARAM_EVENT_PATH_CONFIG file. error:{e}\n")
